﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Beeflys
{
    public abstract partial class AActor
    {
        private readonly Dictionary<Type, AMessenger> messengers = new Dictionary<Type, AMessenger>();
        
        public Task<IEnumerable<AMessenger>> GetMessengersAsync()
        {
            return Task.FromResult<IEnumerable<AMessenger>>(this.messengers.Values);
        }

        public Task<TMessenger> AddMessengerAsync<TMessenger>() where TMessenger : AMessenger, new()
        {
            var messengerType = typeof(TMessenger);
            if (this.messengers.ContainsKey(messengerType))
            {
                return Task.FromResult<TMessenger>(null);
            }

            var messenger = new TMessenger();
            messenger.Constructor(this);
            this.messengers.Add(messengerType, messenger);
            
            return Task.FromResult(messenger);
        }

        public Task<TMessenger> GetMessengerAsync<TMessenger>() where TMessenger : AMessenger, new()
        {
            var messengerType = typeof(TMessenger);
            if (!this.messengers.ContainsKey(messengerType))
            {
                return Task.FromResult<TMessenger>(null);
            }

            return Task.FromResult(this.messengers[messengerType] as TMessenger);
        }
        
        private Task MessengerInternalDestroyAsync()
        {
            this.components.Clear();

            return Task.CompletedTask;
        }
    }
}